package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.pojo.Issue;
import com.cskaoyan.bean.pojo.IssueExample;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.service.admin.IssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Name : IssueServiceImpl.java
 * @Time : 2021/8/13 00:47
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */
@Service
@Transactional
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public BaseRespData list(String question, BaseParamBo baseParamBo) {
        PageHelper.startPage(baseParamBo.getPage(), baseParamBo.getLimit());
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(baseParamBo.getSort() + " " + baseParamBo.getOrder());
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (question != null && !question.equals("")) {
            criteria.andQuestionLike("%" + question + "%");
        }
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        long total = new PageInfo<>(issues).getTotal();
        return BaseRespData.create(issues, total);
    }

    @Override
    public Issue create(Issue issue) {
        Date date = new Date();
        issue.setAddTime(date);
        issue.setUpdateTime(date);
        issue.setDeleted(false);
        issueMapper.insert(issue);
        issue.setId(issue.getId());
        return issue;
    }

    @Override
    public Issue update(Issue issue) {
        Date date = new Date();
        issue.setUpdateTime(date);
        issueMapper.updateByPrimaryKeySelective(issue);
        return issue;
    }

    @Override
    public void delete(Issue issue) {
        issue.setDeleted(true);
        issueMapper.updateByPrimaryKeySelective(issue);
    }
}
